﻿<!DOCTYPE>

<html>

<head>
  <meta http-equiv="X-UA-Compatible" content="IE=EDGE;IE=11;IE=8,chrome=1" />
  <title>快速替换</title>
  <link href="/InSiteWebApplication/Content/Easyui/themes/metro/easyui.css" rel="stylesheet" />
  <link href="/InSiteWebApplication/Content/Easyui/custom-easyui.css" rel="stylesheet" />
  <link href="/InSiteWebApplication/Content/Easyui/themes/icon.css" rel="stylesheet" />
  <link rel="stylesheet" href="/InSiteWebApplication/Content/webuploader/webuploader.css" />

</head>

<body>
  <div class="panelSection">
    <div class="easyui-panel " title="存放路径" style="padding:3px;"
      data-options="closable:false,collapsible:true,minimizable:false,maximizable:false">
      <div style="margin: 3px 0px 3px 3px">
        <div style="">
          <div class="divLabel" style="margin-bottom: 5px; ">
            需要更新的路径：</div>
          <input id="txtTopPath" class="easyui-textbox" name="TopPath" type="text" data-options=""
            style="width: 800px; height: 22px" />
        </div>
        <div style="">
          <div class="divLabel" style="margin-bottom: 5px; ">
            备份路径：</div>
          <input id="txtbackUpDir" class="easyui-textbox" name="TopPath" type="text" data-options=""
            style="width: 800px; height: 22px" />
        </div>
        <div style="">
          <div class="divLabel" style="margin-bottom: 5px; ">
            解压后文件名：</div>
          <input id="txtUnpackDirName" class="easyui-textbox" name="TopPath" type="text" data-options="value:'代码更新'"
            style="width: 800px; height: 22px" />
        </div>
      </div>
    </div>

  </div>
  <div class="panelSection">
    <div class="easyui-panel " title="操作区域" style="padding:3px;"
      data-options="closable:false,collapsible:true,minimizable:false,maximizable:false">
      <div>
        <div class="divLabel">
          选择文件：</div>
        <div id="temUploader" style="margin-top: 5px;" class="wu-example">
          <div style="float: left">
            <div id="tempCodeFiles" style="float: left; margin-left: 0px; margin-top: 0px">
              上传并解压</div>
            <span style="color:red;font-weight:bold;margin-left: 10px;">内部系统，严禁上传涉密信息</span>
          </div>
        </div>
      </div>
    </div>
  </div>

  <div id="dlg" class="easyui-dialog" title="替换的文件" data-options="closed:true"
    style="width:100%; height: 600px; padding: 10px">
    <ul id="dl" class="easyui-datalist" title="" style="width:100%;height:500px"></ul>
    <div style="text-align:center">
      <button id="btnReplace" class="easyui-linkbutton" data-options=""
        style="width:150px;height:40px;font-size:18px;padding-bottom:5px">
        确认替换</button>
    </div>
  </div>

  <div id="hdDiv">
    <input id="hdUnpackDir" type="hidden" value=" " />
  </div>
</body>

</html>
<script src="/InSiteWebApplication/Scripts/CustomPageJs/ReferenceJsLib.js" type="text/javascript"></script>
<script src="/InSiteWebApplication/Content/Easyui/jquery.min.js" type="text/javascript"></script>
<script src="/InSiteWebApplication/Content/Easyui/jquery.easyui.min.js" type="text/javascript"></script>
<script src="/InSiteWebApplication/Content/Easyui/easyui-lang-zh_CN.js" type="text/javascript"></script>
<script src="/InSiteWebApplication/Scripts/CustomPageJs/CustomCommon.js" type="text/javascript"></script>
<script type="text/javascript" src="/InSiteWebApplication/Content/webuploader/webuploader.js"></script>
<script type="text/javascript">
  var page = {
    rootPath: getRootPath(), //网站根路径
    ajaxUrl: getRootPath() + "/Ashx/UploadFileSave.ashx",
    //初始化
    init: function () {
      page.uploadCodeFiles();
      //初始值
      $("#txtTopPath").textbox('setValue', 'C:\\Program Files (x86)\\Camstar\\InSite Web Application');
      $("#txtbackUpDir").textbox('setValue', 'C:\\Program Files (x86)\\Camstar\\InSite Web Application\\InSiteWebApplication\\Temp2\\ExportTemp');

      //替换文件
      $('#btnReplace').on("click", function () {
        page.replaceFiles();
      });
      //初始化datalist
      $("#dl").datalist({
        lines: true, valueField: 'value', textField: 'text', groupField: 'group',
        groupFormatter: function (value, rows) {
          return "<span style='white-space:normal;text-align: justify;text-justify: inter-word;word-break: break-all;' >" + value + "--" + rows.length + "项" + "</span>";
        },
        nowrap: false,
        textFormatter: function (value, row, index) {
          if (row.type == 0)//失败
          {
            return "<span style='white-space:normal;text-align: justify;text-justify: inter-word;word-break: break-all;color:red' >" + value + " 未找到路径，失败!</span>";
          } else if (row.type == 1) {
            return "<span style='white-space:normal;text-align: justify;text-justify: inter-word;word-break: break-all;' >" + value + " 替换成功!</span>";
          } else {
            return "<span style='white-space:normal;text-align: justify;text-justify: inter-word;word-break: break-all;' >" + value + "</span>";
          }
        }
      });
    }
    , //上传代码文件
    uploadCodeFiles: function () {
      var temUploader = WebUploader.create({
        // swf文件路径
        swf: page.rootPath + "/Content/webuploader/Uploader.swf",
        // 文件接收服务端。
        server: page.rootPath + "/Ashx/UploadFileSave.ashx",
        // 内部根据当前运行是创建，可能是input元素，也可能是flash.
        pick: '#tempCodeFiles',
        // 不压缩image, 默认如果是jpeg，文件上传前会压缩一把再上传！
        resize: false,
        formData: {
          action: "filesUnpack",
          unpackDirName: $("#txtUnpackDirName").textbox('getValue')
        },
        accept: {
          title: '压缩文件',
          extensions: 'zip',
          mimeTypes: '.zip'
        },
        multiple: false
      });
      // 当有文件被添加进队列的时候
      temUploader.on('fileQueued', function (file) {
        $("#hdUnpackDir").val('');
        if (isNullOrWhiteSpaceStr($("#txtUnpackDirName").textbox('getValue'))) {

          var f = temUploader.getFile(file.id); //找到队列中的文件
          temUploader.removeFile(f); //删除掉队列中的文件

          displayMessage("请输入必填项", false);
          return;
        }
        temUploader.upload();

      });
      //关闭弹窗
      $('#dlg').dialog({
        onClose: function () {
          var fs = temUploader.getFiles(); //找到队列中的所有文件
          for (var i = 0; i < fs.length; i++) {
            var id = fs[i].id;
            if ($("#" + id).length > 0) {
              var f = temUploader.getFile(id); //找到队列中的文件
              temUploader.removeFile(f, true); //删除掉队列中的所有文件
              $("#" + id).remove(); //删除该div
            }
          }
        }
      });
      //上传成功
      temUploader.on('uploadSuccess', function (file, response) {
        $.messager.progress('close');
        var f = temUploader.getFile(file.id); //找到队列中的文件
        temUploader.removeFile(f); //删除掉队列中的文件
        $('#' + file.id).remove();
        //console.log(response);
        var result = response;
        if (result.Result) {
          //弹出框内列出文件
          $("#dlg").dialog('open');
          $("#hdUnpackDir").val(Object.keys(result.ReturnData)[0]);

          var dlData = [];
          result.ReturnData[$("#hdUnpackDir").val()].forEach(function (item, index) {
            dlData.push({ value: index, text: item, group: item.substr(0, item.lastIndexOf("\\")) });
          });
          $("#dl").datalist('loadData', dlData);

          displayMessage("上传并解压成功,共：" + dlData.length + "个文件", true);
        } else {
          displayMessage(result.Message, false);
        }


      });
      //上传失败
      temUploader.on('uploadError', function (file, response) {
        $("#dlg").dialog('close');
        $.messager.progress('close');
        var result = JSON.parse(response);
        displayMessage(result.Message, false);
      });
      //文件上传过程中创建进度条实时显示。
      temUploader.on('uploadProgress', function (file, percentage) {
        $.messager.progress({ text: '', msg: '处理中...' });


      });
    }
    , //替换文件
    replaceFiles: function () {
      var param = {
        action: "replaceFiles",
        sourceDir: $("#hdUnpackDir").val(),
        targetDir: $("#txtTopPath").textbox('getValue'),
        backDir: $("#txtbackUpDir").textbox('getValue')
      };
      if (isNullOrWhiteSpaceStr(param.sourceDir) || isNullOrWhiteSpaceStr(param.targetDir) || isNullOrWhiteSpaceStr(param.backDir)) {
        displayMessage("请输入必填项", false);
        return;
      }

      $.ajax({
        type: "Post",
        url: page.ajaxUrl,
        data: param,
        cashe: false,
        async: true,
        beforeSend: function () {
          $.messager.progress({ text: '', msg: '处理中...' });
        },
        success: function (data) {
          //console.log(data);
          $.messager.progress('close');
          var result = JSON.parse(data);
          if (result.Result) {
            //给分页对象赋值
            var returnData = result.ReturnData;
            var dlData = [];
            result.ReturnData['0'].forEach(function (item, index) {
              dlData.push({ type: 0, value: index, text: item, group: item.substr(0, item.lastIndexOf("\\")) });
            });
            result.ReturnData['1'].forEach(function (item, index) {
              dlData.push({ type: 1, value: index, text: item, group: item.substr(0, item.lastIndexOf("\\")) });
            });
            $("#dl").datalist('loadData', dlData);
            displayMessage("替换完毕,成功共：" + result.ReturnData['1'].length + "个文件，失败共：<span style='color:red'>" + result.ReturnData['0'].length + "</span>个文件", true);

          } else {
            displayMessage(result.Message, false);
          }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
          alert(XMLHttpRequest.responseText);
        }
      });
    }
  }

  $(function () {
    page.init();
  })
</script>